package script.mainpage
{
	import laya.components.Script;
	import laya.events.Event;
	import laya.ui.View;
	import laya.utils.Handler;
	
	import model.HttpErrorCode;
	import model.HttpRequestUtil;
	import model.Userdata;
	import model.order.OrderVo;
	
	import script.ViewManager;
	import script.mainpage.items.UserOrderCell;
	
	import ui.tabview.UserCenterPanelUI;
	
	import utils.UtilTool;
	
	public class UserCenterController extends Script
	{
		private var uiSkin:UserCenterPanelUI;
		
		private var btnlist:Array;
		
		public function UserCenterController()
		{
			super();
		}
		
		override public function onStart():void
		{
			uiSkin = this.owner as UserCenterPanelUI;
			
			uiSkin.orderList.itemRender = UserOrderCell;
			
			uiSkin.orderList.vScrollBarSkin = "";
			uiSkin.orderList.spaceY = 10;
			uiSkin.orderList.renderHandler = new Handler(this, updateOrderList);
			uiSkin.orderList.selectEnable = false;
			
			uiSkin.orderList.array = [];
			
			this.uiSkin.nopay.selected = true;

			 btnlist = [uiSkin.nopay,uiSkin.haspay,uiSkin.cancels,uiSkin.allorder];
			
			for(var i=0;i < btnlist.length;i++)
			{
				btnlist[i].on(Event.CLICK,this,onRefreshOrder,[i]);
			}
			uiSkin.loginBtn.on(Event.CLICK,this,function(){
				
				ViewManager.instance.openView(ViewManager.VIEW_LOGIN_PANEL,true);
				
			})
				
			uiSkin.logOut.on(Event.CLICK,this,function(){
				
				HttpRequestUtil.instance.Request(HttpRequestUtil.httpUrl + HttpRequestUtil.loginOutUrl,this,onLogOut,null,"post",null);

				Userdata.instance.resetData();
				//ViewManager.instance.openView(ViewManager.VIEW_LOGIN_PANEL,true);
				
			})
				
				
			if(Userdata.instance.isLogin)
			{
				uiSkin.accountLbl.text = Userdata.instance.userName + "  " + Userdata.instance.userAccount;
				
				uiSkin.loginBtn.visible = false;
				uiSkin.logOut.visible = true;
				
				var headers:Array = ["Content-Type", "application/json;charset=UTF-8"];
				
				var psotdata:String = JSON.stringify({"orderState":0});//"us
				
				HttpRequestUtil.instance.Request(HttpRequestUtil.httpUrl + HttpRequestUtil.getClientUserOrderList,this,onOrdersBack,psotdata,"post",null,true,headers);
				
			}
			else
			{
				uiSkin.accountLbl.text = "未登录";
				
				uiSkin.loginBtn.visible = true;
				uiSkin.logOut.visible = false;
			}

			
		}
		
		private function updateOrderList(cell:UserOrderCell):void
		{
			cell.setData(cell.dataSource);
		}
		
		private function onLogOut(data:*):void
		{
			var result:Object = JSON.parse(data as String);
			
			if(result.code == HttpErrorCode.OK)
			{
				Userdata.instance.resetData();
				Userdata.instance.isLogin = false;
				UtilTool.setLocalVar("useraccount","");
				UtilTool.setLocalVar("userpwd","");
				
				ViewManager.instance.openView(ViewManager.VIEW_LOGIN_PANEL,true);

			}
			
		}
		private function onOrdersBack(data:Object):void
		{
			if(uiSkin.destroyed)
				return;
			var result:Object = JSON.parse(data as String);
			
			if(result.code == HttpErrorCode.OK)
			{
				var goodslist:Array = [];
				for(var i:int=0;i < result.data.length;i++)
				{
					goodslist.push(new OrderVo(result.data[i]));
				}
				
				uiSkin.orderList.array = goodslist;
			}
			
		}
		
		private function onRefreshOrder(index:int):void
		{
			var state:Array = [0,1,1,1];
			
			for(var i=0;i < btnlist.length;i++)
				btnlist[i].selected = i == index;
			
			var headers:Array = ["Content-Type", "application/json;charset=UTF-8"];
			
			var psotdata:String = JSON.stringify({"orderState":state[index]});//"us
			
			HttpRequestUtil.instance.Request(HttpRequestUtil.httpUrl + HttpRequestUtil.getClientUserOrderList,this,onOrdersBack,psotdata,"post",null,true,headers);
			
		}
		
	}
}